<?php /* PROJECTS $Id: addedit.php,v 1.93.6.9 2007/02/02 18:11:04 merlinyoda Exp $ */
$project_id = intval( dPgetParam( $_GET, "project_id", 0 ) );
$company_id = intval( dPgetParam( $_GET, "company_id", 0 ) );
$contact_id = intval( dPgetParam( $_GET, "contact_id", 0 ) );

$perms =& $AppUI->acl();
// check permissions for this record
$canEdit = $perms->checkModuleItem( $m, 'edit', $project_id );
$canAuthor = $perms->checkModuleItem( $m, 'add' );
if ((!$canEdit && $project_id > 0) || (!$canAuthor && $project_id == 0)) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}

// get a list of permitted companies
require_once( $AppUI->getModuleClass ('companies' ) );

$row = new CCompany();
$companies = $row->getAllowedRecords( $AppUI->user_id, 'company_id,company_name', 'company_name' );
$companies = arrayMerge( array( '0'=>'' ), $companies );

// pull users
$q  = new DBQuery;
$q->addTable('users','u');
$q->addTable('contacts','c');
$q->addQuery('user_id');
$q->addQuery('CONCAT_WS(" ",contact_first_name,contact_last_name)');
$q->addOrder('contact_last_name');
$q->addWhere('u.user_contact = c.contact_id');
$q->addWhere('user_id <> 1');
$users = $q->loadHashList();

// load the record data
$row = new CProject();

if ($project_id > 0 && !$row->load( $project_id, false )) {
	$AppUI->setMsg( 'Project' );
	$AppUI->setMsg( "invalidID", UI_MSG_ERROR, true );
	$AppUI->redirect();
} else if (count( $companies ) < 2 && $project_id == 0) {
	$AppUI->setMsg( "noCompanies", UI_MSG_ERROR, true );
	$AppUI->redirect();
}

if ($project_id == 0 && $company_id > 0) {
	$row->project_company = $company_id;
}

// add in the existing company if for some reason it is dis-allowed
if ($project_id && !array_key_exists( $row->project_company, $companies )) {
	$q  = new DBQuery;
	$q->addTable('companies');
	$q->addQuery('company_name');
	$q->addWhere('companies.company_id = '.$row->project_company);
	$companies[$row->project_company] = $q->loadResult();
}

// get critical tasks (criteria: task_end_date)
$criticalTasks = ($project_id > 0) ? $row->getCriticalTasks() : NULL;

// get ProjectPriority from sysvals
$projectPriority = dPgetSysVal( 'ProjectPriority' );

// format dates
$df = $AppUI->getPref('SHDATEFORMAT');

$start_date = new CDate( $row->project_start_date );

$end_date = intval( $row->project_end_date ) ? new CDate( $row->project_end_date ) : null;
$actual_end_date = intval( $criticalTasks[0]['task_end_date'] ) ? new CDate( $criticalTasks[0]['task_end_date'] ) : null;
$style = (( $actual_end_date > $end_date) && !empty($end_date)) ? 'style="color:red; font-weight:bold; width: 70px"' : '';

// setup the title block
$titleBlock = new CTitleBlock( $project_id > 0 ? 'Edit Project' : 'New Project' );
$titleBlock->addButton( 'projects list', 'index.php?m=projects' );
$titleBlock->addCell();
$titleBlock->show();

//Build display list for departments
$company_id = $row->project_company;

// Build Project member list
$project_contacts = $project_id > 0 ? $row->getMembers() :
	array($AppUI->user_id => $AppUI->user_first_name.' '.$AppUI->user_last_name);

$project_status = $project_id > 0 ? $row->project_status : $_REQUEST['project_status'];
?>
<script language="javascript">
Calendar.enabled = true;

function setColor(color) {
	var f = document.editFrm;
	if (color) {
		f.project_color_identifier.value = color;
	}
	$('test').style.background = '#' + f.project_color_identifier.value;
	//fix for mozilla: does this work with ie? opera ok.
}

function setShort() {
	var f = document.editFrm;
	var x = 10;
	if (f.project_name.value.length < 11) {
		x = f.project_name.value.length;
	}
	if (f.project_short_name.value.length == 0) {
		f.project_short_name.value = f.project_name.value.substr(0,x);
	}
}

function doSubmit() {
	var f = document.editFrm;
	var msg = '';

	f.project_name.value = f.project_name.value.trim();
	f.project_short_name.value = f.project_short_name.value.trim();

	<?php
	/*
	** Automatic required fields generated from System Values
	*/
	$requiredFields = dPgetSysVal( 'ProjectRequiredFields' );
	echo dPrequiredFields($requiredFields);
	?>

	if (msg.length < 1) {
		MemberList('idMembers').selectAll();
		f.submit();
	} else {
		alert(msg);
	}
}

function doCancel()
{
	if (confirm('Are you sure you want to cancel.')) {
		go('?<?=$AppUI->getPlace()?>');
	}
}

function changeManager(list)
{
	var mlist = $('idMembers');
	var alist = $('idAvailables');
	var i, m = list.options[list.selectedIndex].value;
	for (i = 0; i < mlist.options.length; i++)
		if (mlist.options[i].value == m) {
			break;
		}

	if (i >= mlist.options.length) {
		mlist.options[i] = new Option(list.options[list.selectedIndex].text, m);
	}

	for (i = 0; i < alist.options.length; i++)
		if (alist.options[i].value == m) {
			alist.options[i] = null;
			break;
		}
}
</script>
<form name="editFrm" action="index.php?m=projects" method="post">
	<input type="hidden" name="dosql" value="do_project_aed" />
	<input type="hidden" name="project_id" value="<?=$project_id;?>" />
	<input type="hidden" name="project_creator" value="<?=($project_id > 0) ? $row->project_creator : $AppUI->user_id;?>" />
<table cellspacing="0" cellpadding="4" border="0" width="100%">
<tr>
	<td class="tabon">Project <?=$row->project_name?> Information</td>
	<td width="100%">&nbsp;</td>
</tr>
</table>
<table cellspacing="0" cellpadding="4" border="0" width="100%" class="infopanel">
<tr>
	<td>
		<?
		if ($project_id > 0) {
			showButton( "view", "?m=projects&a=view&project_id=$project_id" );
		}
		?>
		<input class="button" type="button" name="btnFuseAction2" value="<?=$AppUI->_('save');?>" onClick="doSubmit()" />
		<input class="button" type="button" name="cancel2" value="<?=$AppUI->_('cancel');?>" onClick="doCancel()" />
	</td>
	<td align="right">
		<?php
		if ($project_id == 0) {
			$q  = new DBQuery();
			$q->addTable('projects', 'p');
			$q->addTable('tasks', 't');
			$q->addQuery('p.project_id, p.project_name');
			$q->addWhere('t.task_project = p.project_id');
			$q->addWhere('p.project_status = 7');
			$q->addOrder('p.project_name');

			$importList = $q->loadHashList ();
			if (count($importList)) {
				echo $AppUI->_('Import tasks from').':';
				$importList = arrayMerge( array( '0'=> $AppUI->_('None') ), $importList);
				echo arraySelect( $importList, 'import_tasks_from', 'size="1" class="text"', null, false );
			}
		}
		?>
	</td>
</tr>
</table>
<table cellspacing="0" cellpadding="4" border="0" width="100%" class="subinfopanel">
<tr>
	<td valign="top">
		<table cellspacing="0" cellpadding="2" border="0">
		<tr>
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Name')?></td>
			<td nowrap="nowrap" colspan="3">
				<input type="text" name="project_name" value="<?=dPformSafe( $row->project_name );?>"
					style="width: 270px" maxlength="50" onBlur="setShort();" class="text"/>
			</td>
		</tr>
		<tr>
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Alias');?></td>
			<td>
				<input type="text" name="project_short_name" value="<?=dPformSafe( @$row->project_short_name ) ;?>"
						style="width: 90px" maxlength="10" class="text" />
			</td>
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Type');?></td>
			<td>
				<?=arraySelect( $ptype, 'project_type', 'style="width: 95px" size="1" class="text"', $row->project_type, true );?>
			</td>
		</tr>
		<tr>
			<td align="right" nowrap="nowrap"><?=$AppUI->_( 'Priority' );?></td>
			<td>
				<?=arraySelect( $projectPriority, 'project_priority', 'size="1" class="text" style="width: 95px"', $row->project_priority, true );?>
			</td>
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Budget');?> <?=$dPconfig['currency_symbol'] ?></td>
			<td>
				<input type="text" name="project_target_budget" value="<?=@$row->project_target_budget;?>"
					maxlength="10" class="text" style="width: 90px"/>
			</td>
		</tr>
		<tr>
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Start Date');?></td>
			<td nowrap="nowrap">
				<input id="idDatePStart" type="hidden" name="project_start_date" value="<?=$start_date->format( FMT_TIMESTAMP_DATE );?>" />
				<input id="idDatePStartD" type="text" class="text" name="start_date"
					value="<?=$start_date->format( $df );?>" disabled="disabled"
					style="width: 70px"/>
				<img id="idDatePStartB" src="images/calendar.gif" title="<?=$AppUI->_('Calendar');?>" align="absmiddle"/>
			</td>
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Target Date');?></td>
			<td nowrap="nowrap">
				<input id="idDatePEnd" type="hidden" name="project_end_date" value="<?=$end_date ? $end_date->format( FMT_TIMESTAMP_DATE ) : '';?>" />
				<input id="idDatePEndD" type="text" class="text" name="end_date"
					value="<?=$end_date ? $end_date->format( $df ) : '';?>" disabled="disabled"
					style="width: 70px"/>
				<img id="idDatePEndB" src="images/calendar.gif" title="<?=$AppUI->_('Calendar');?>" align="absmiddle"/>
			</td>
		</tr>
		<? if ($project_id > 0) { ?>
		<tr>
			<td>&nbsp;</td>
			<td align="right" nowrap="nowrap" colspan="2"><?=$AppUI->_('Actual Finish Date');?></td>
			<td nowrap="nowrap" colspan="100%">
			<?php
			if ($project_id > 0) {
				echo $actual_end_date ? '<a href="?m=tasks&a=view&task_id='.$criticalTasks[0]['task_id'].'">' : '';
				echo $actual_end_date ? '<div class="text" '. $style.'>'.$actual_end_date->format( $df ).'</div>' : '<div class="text" style="width: 70px">-</div>';
				echo $actual_end_date ? '</a>' : '';
			} else {
				echo $AppUI->_('Dynamically calculated');
			}
			?>
			</td>
		</tr>
		<? } ?>
		<tr>
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Customer');?></td>
			<td colspan="100%" nowrap="nowrap">
			<?=arraySelect( $companies, 'project_company', 'class="text" size="1" style="width: 275px"', $row->project_company );?>
			</td>
		</tr>
		<tr>
			<td align="right" valign="top">
				<?=$AppUI->_('Description');?>
			</td>
			<td colspan="100%">
				<textarea name="project_description" style="width: 270px; height: 50px" class="textarea"><?=dPformSafe( @$row->project_description );?></textarea>
			</td>
		</tr>
		<tr style="display: none">
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Actual Budget');?> <?=$dPconfig['currency_symbol'] ?></td>
			<td>
				<input type="text" name="project_actual_budget" value="<?=@$row->project_actual_budget;?>" size="10" maxlength="10" class="text"/>
			</td>
		</tr>
		<!--tr>
			<td align="right" nowrap="nowrap"><?=$AppUI->_('URL');?></td>
			<td colspan="2">
				<input type="text" name="project_url" value='<?=@$row->project_url;?>' size="40" maxlength="255" class="text" />
			</td>
		</tr>
		<tr>
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Staging URL');?></td>
			<td colspan="2">
				<input type="Text" name="project_demo_url" value='<?=@$row->project_demo_url;?>' size="40" maxlength="255" class="text" />
			</td>
		</tr>
		<tr>
			<td align="right" colspan="3">
			<?php
				require_once("./classes/CustomFields.class.php");
				$custom_fields = new CustomFields( $m, $a, $row->project_id, "edit" );
				$custom_fields->printHTML();
			?>
			</td>
		</tr-->
		</table>
	</td>
	<td width="100%" valign="top">
		<table cellspacing="0" cellpadding="2" border="0" width="100%">
		<tr>
			<td align="right" nowrap="nowrap" width="50px"><?=$AppUI->_('Manager');?></td>
			<td width="100%">
			<?=arraySelect( $users, 'project_owner', 'size="1" class="text" style="width: 100%" onchange="changeManager(this)"',$row->project_owner ? $row->project_owner : $AppUI->user_id )?>
			</td>
		</tr>
		<tr>
			<td colspan="2" width="100%">
				<table cellspacing="2" cellpadding="0" border="0" width="100%">
				<tr>
					<td>Project Members</td>
					<td width="20px">&nbsp;</td>
					<td>Available Users</td>
				</tr>
				<tr>
					<td width="45%">
					<?=arraySelect( $project_contacts, 'project_contacts[]', 'id="idMembers" size="10" class="text" style="width: 100%; height: 150px" multiple="true"', 0) ?>
					</td>
					<td align="center" class="internal">
						<input type="image" src="images/prev.gif" class="button"
							onclick="UserList('idMembers', 'idAvailables').join(); return false;"/><br/><br/>
						<input type="image" src="images/next.gif" class="button"
							onclick="UserList('idMembers', 'idAvailables').part(); return false;"/>
					</td>
					<td width="44%">
					<?php
						foreach ($project_contacts as $id => $v) unset($users[$id]);
						echo arraySelect( $users, 'available_users', 'id="idAvailables" size="10" class="text" style="width: 100%; height: 150px" multiple="true"', 0)
					?>
					</td>
				</tr>
				</table>
			</td>
		</tr>
		<tr style="display: none">
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Color Identifier');?></td>
			<td nowrap="nowrap">
				<input type="text" name="project_color_identifier" value="<?=(@$row->project_color_identifier) ? @$row->project_color_identifier : 'FFFFFF';?>" size="10" maxlength="6" onBlur="setColor();" class="text" /> *
			</td>
			<td nowrap="nowrap" align="right">
				<a href="#" onClick="newwin=window.open('./index.php?m=public&a=color_selector&dialog=1&callback=setColor', 'calwin', 'width=320, height=300, scrollbars=no');"><?php echo $AppUI->_('change color');?></a>
			</td>
			<td nowrap="nowrap">
				<span id="test" title="test" style="background:#<?=(@$row->project_color_identifier) ? @$row->project_color_identifier : 'FFFFFF';?>;"><a href="#" onClick="newwin=window.open('./index.php?m=public&a=color_selector&dialog=1&callback=setColor', 'calwin', 'width=320, height=300, scrollbars=no');"><img src="./images/shim.gif" border="1" width="40" height="20" /></a></span>
			</td>
		</tr>
		<input type="hidden" name="project_status" value="<?=$project_status?>"/>
		</table>
	</td>
</tr>
</form>
</table>
